package com.bf.electroplating.controller;

import com.bf.electroplating.pojo.vo.ResultVO;
import com.bf.electroplating.service.IRainfallWeatherDataSyncService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 雨排口天气数据同步控制器
 *
 * @author system
 * @since 2025-01-11
 */
@Slf4j
@RestController
@RequestMapping("/api/rainfall-weather-sync")
@Tag(name = "雨排口天气数据同步", description = "雨排口天气数据同步相关接口")
public class RainfallWeatherDataSyncController {

    @Autowired
    private IRainfallWeatherDataSyncService rainfallWeatherDataSyncService;

    /**
     * 手动触发所有站点雨排口天气数据同步
     */
    @PostMapping("/sync-all")
    @Operation(summary = "同步所有站点雨排口天气数据", description = "手动触发同步所有站点的雨排口天气数据")
    public ResultVO<String> syncAllRainfallWeatherData() {
        try {
            log.info("手动触发同步所有站点雨排口天气数据");
            rainfallWeatherDataSyncService.syncAllRainfallWeatherDataThisHour();
            return ResultVO.ok("所有站点雨排口天气数据同步完成");
        } catch (Exception e) {
            log.error("手动同步所有站点雨排口天气数据失败", e);
            return ResultVO.fail("同步失败: " + e.getMessage());
        }
    }

    /**
     * 手动触发指定站点雨排口天气数据同步
     */
    @PostMapping("/sync-by-site/{siteId}")
    @Operation(summary = "同步指定站点雨排口天气数据", description = "手动触发同步指定站点的雨排口天气数据")
    public ResultVO<String> syncRainfallWeatherDataBySite(
            @Parameter(description = "站点ID", required = true)
            @PathVariable("siteId") Integer siteId) {
        try {
            log.info("手动触发同步站点 {} 的雨排口天气数据", siteId);
            rainfallWeatherDataSyncService.syncRainfallWeatherDataBySiteId(siteId);
            return ResultVO.ok("站点 " + siteId + " 的雨排口天气数据同步完成");
        } catch (Exception e) {
            log.error("手动同步站点 {} 的雨排口天气数据失败", siteId, e);
            return ResultVO.fail("同步失败: " + e.getMessage());
        }
    }

    /**
     * 手动触发指定时间范围雨排口天气数据同步
     */
    @PostMapping("/sync-by-time-range")
    @Operation(summary = "同步指定时间范围雨排口天气数据", description = "手动触发同步指定时间范围的雨排口天气数据")
    public ResultVO<String> syncRainfallWeatherDataByTimeRange(
            @Parameter(description = "站点ID", required = true)
            @RequestParam("siteId") Integer siteId,
            @Parameter(description = "开始时间", required = true, example = "2025-05-31 00:00:00")
            @RequestParam("beginTime") String beginTime,
            @Parameter(description = "结束时间", required = true, example = "2025-06-02 00:00:00")
            @RequestParam("endTime") String endTime) {
        try {
            log.info("手动触发同步站点 {} 在 {} 到 {} 时间范围内的雨排口天气数据", siteId, beginTime, endTime);
            rainfallWeatherDataSyncService.syncRainfallWeatherDataByTimeRange(siteId, beginTime, endTime);
            return ResultVO.ok("站点 " + siteId + " 在指定时间范围内的雨排口天气数据同步完成");
        } catch (Exception e) {
            log.error("手动同步站点 {} 在指定时间范围内的雨排口天气数据失败", siteId, e);
            return ResultVO.fail("同步失败: " + e.getMessage());
        }
    }
}
